def make_geno_table(dataset, dbname, alleletype, jobs, shard, test):
    import sys, os
    from sqlalchemy import MetaData
    from utils import get_conf, pg_url, randstr
    from dbutils import get_db_type
    conf = get_conf()
    dbuser = conf["dbuser"]
    password = conf["password"]
    portnum = conf["portnum"]
    dbstring = "%s://%s:%s@localhost:%s/%s"%(pg_url(), dbuser, password, portnum, dbname)
    dbtype = get_db_type(dbstring)
    if dbtype not in conf.sections:
        sys.exit("dbtype must be one of %s, not %s.\nWas the database %s created with init_db.py?"%(', '.join(conf.sections), dbtype, dbstring_dbname))
    dirpath = os.path.dirname(os.path.abspath(__file__))
    datasets = conf[dbtype].sections
    if dataset not in datasets:
        sys.exit("error, dataset %s is not listed in the config as one of the datasets associated with database type %s"%(dataset, dbtype))
    rstr = randstr()
    tmpdir = os.path.join(conf['tmpdir'], 'tmp_%s'%rstr)
    annotfile = conf[dbtype]["annotfile"]
    phenofiles = conf[dbtype][dataset]["phenofiles"]
    if test:
        genofile = os.path.basename(conf[dbtype][dataset]["genofile"]) + '.trunc'
        genofile = os.path.join(tmpdir, genofile)
        os.system("python truncate_genofile.py %s %s %s"%(tmpdir, dataset, dbname)) and sys.exit()
    else:
        genofile = conf[dbtype][dataset]["genofile"]

    if shard and test:
        schema = dataset + "_shard_test"
    elif shard:
        schema = dataset + "_shard"
    elif test:
        schema = dataset + "_test"
    else:
        schema = dataset

    meta = MetaData()
    if dbtype == 'affy6':
        import geno
        if options.shard:
            from dbschema import make_tables_shard_affy6
            table_dict = make_tables_shard_affy6(schema)
            geno.Geno_Shard_Affy6(genofile, schema, dbstring, tmpdir, table_dict, options.jobs).make_geno()
        else:
            from dbschema import make_tables_affy6
            table_dict = make_tables_affy6(schema)
            geno.Geno_Single_Affy6(genofile, schema, dbstring, tmpdir, table_dict, options.jobs).make_geno()
    elif dbtype == 'illumina':
        import geno
        if options.shard:
            from dbschema import make_tables_shard_illumina
            table_dict = make_tables_shard_illumina(schema)
            geno.Geno_Shard_Illumina(genofile, schema, dbstring, tmpdir, table_dict, options.alleletype, options.jobs).make_geno()
        else:
            from dbschema import make_tables_illumina
            table_dict = make_tables_illumina(schema)
            geno.Geno_Single_Illumina(genofile, schema, dbstring, tmpdir, table_dict, options.alleletype, options.jobs).make_geno()

if __name__=="__main__":
    import sys, os
    from optparse import OptionParser
    usr = os.popen('whoami').read().strip()
    usage = "'%prog [-a alleletype] dataset db'. Using the value dbtype_" + usr + " for db is recommended, where dbtype can be either 'affy6' or 'illumina'. Alleletype option is only required if dbtype is 'illumina', with possible values 'forward' and 'top' and default 'forward'."
    parser = OptionParser(usage=usage)
    parser.add_option("-a", "--allele-type", action="store", type="string", dest="alleletype", help="allele type")
    parser.add_option("-j", "--jobs", action="store", type="int", dest="jobs", help="number of jobs to run in parallel")
    parser.add_option("-s", "--sharding", action="store_true", dest="shard", help="use geno table sharding")
    parser.add_option("-t", "--test", action="store_true", dest="test", help="test version")
    parser.set_defaults(jobs=1)
    (options, args) = parser.parse_args()
    if len(args) != 2:
        print "incorrect number of arguments"
        parser.print_help()
        sys.exit(1)
    dataset = args[0]
    dbname = args[1]
    make_geno_table(dataset, dbname, options.alleletype, options.jobs, options.shard, options.test)
